package com.forlove.app.common.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class GuideWindow {

	private PopupWindow mWindow;

	private Context mContext;
	private GuideWindowConfig mConfig;
	private static Handler sHandler = new Handler(Looper.getMainLooper());

	protected GuideWindow(Context context, GuideWindowConfig config) {
		mWindow = new PopupWindow(context);
		mWindow.setBackgroundDrawable(new BitmapDrawable());
		mContext = context;
		mConfig = config;
	}

	public void showAsDropDown(View anchor) {
		if (preShow()) {
			try {
				mWindow.showAsDropDown(anchor);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	public void showAsDropDown(View anchor, int xoff, int yoff) {
		if (preShow()) {
			try {
				mWindow.showAsDropDown(anchor, xoff, yoff);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	public void showAtLocation(View parent, int gravity, int x, int y) {
		if (preShow()) {
			try {
				mWindow.showAtLocation(parent, gravity, x, y);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	public void dismiss() {
		try {
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
				mWindow.setFocusable(false);
			}
			mWindow.dismiss();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public boolean isShowing() {
		return mWindow.isShowing();
	}
	
	public void setOnDismissListener(OnDismissListener onDismissListener) {
		mWindow.setOnDismissListener(onDismissListener);
	}

	private boolean preShow() {
		if (mContext == null || mWindow == null
				|| mConfig.pmContentView == null) {
			return false;
		}
		if (mWindow.isShowing()) {
			try {
				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
					mWindow.setFocusable(false);
				}
				mWindow.dismiss();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		mConfig.pmContentView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
						mWindow.setFocusable(false);
					}
					mWindow.dismiss();
					if (mConfig.pmOnClickListener != null) {
						mConfig.pmOnClickListener.onClick(v);
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		mWindow.setTouchable(mConfig.pmTouchable);
		mWindow.setFocusable(mConfig.pmFocusable);
		mWindow.setOutsideTouchable(mConfig.pmOutsideTouchable);
		mWindow.setWidth(mConfig.pmContentView.getLayoutParams().width);
		mWindow.setHeight(mConfig.pmContentView.getLayoutParams().height);
		mWindow.setContentView(mConfig.pmContentView);
		mWindow.setOnDismissListener(mConfig.pmOnDismissListener);
		mWindow.setAnimationStyle(mConfig.animationStyle);
		return true;
	}

	protected void setTouchable(boolean touchable) {
		mWindow.setTouchable(touchable);
	}

	protected void setFocusable(boolean focusable) {
		mWindow.setFocusable(focusable);
	}

	protected void setOutsideTouchable(boolean touchable) {
		mWindow.setOutsideTouchable(touchable);
	}

	public static class Builder {

		private Context mContext;
		private GuideWindowConfig mConfig;

		public Builder(Context context) {
			mContext = context;
			mConfig = new GuideWindowConfig();
		}

		public Builder setContentView(View contentView) {
			mConfig.pmContentView = contentView;
			return this;
		}

		public Builder setOnClickListener(OnClickListener onClickListener) {
			mConfig.pmOnClickListener = onClickListener;
			return this;
		}

		public Builder setOnDismissListener(OnDismissListener onDismissListener) {
			mConfig.pmOnDismissListener = onDismissListener;
			return this;
		}

		public Builder setTouchable(boolean touchable) {
			mConfig.pmTouchable = touchable;
			return this;
		}

		public Builder setFocusable(boolean focusable) {
			mConfig.pmFocusable = focusable;
			return this;
		}

		public Builder setOutsideTouchable(boolean touchable) {
			mConfig.pmOutsideTouchable = touchable;
			return this;
		}

		public GuideWindow showAsDropDown(View anchor) {
			GuideWindow window = create();

			window.showAsDropDown(anchor);
			return window;
		}

		public void setAnimationStyle(int animationStyle) {
			mConfig.animationStyle = animationStyle;
		}

		public GuideWindow showAsDropDown(View anchor, int xoff, int yoff) {
			GuideWindow window = create();

			window.showAsDropDown(anchor, xoff, yoff);
			return window;
		}

		public GuideWindow showAtLocation(View parent, int gravity, int x, int y) {
			GuideWindow window = create();

			window.showAtLocation(parent, gravity, x, y);
			return window;
		}

		public GuideWindow showAtLocationWithAutoDismiss(View parent,
				int gravity, int x, int y, int duration) {
			final GuideWindow window = create();
			window.showAtLocation(parent, gravity, x, y);

			sHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					if (window != null) {
						try {
							if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
								window.setFocusable(false);
							}
							window.dismiss();
						} catch (Exception e) {

						}
					}

				}
			}, duration);

			return window;
		}

		private GuideWindow create() {
			return new GuideWindow(mContext, mConfig);
		}
	}

	public static class GuideWindowConfig {
		View pmContentView;
		OnClickListener pmOnClickListener;
		OnDismissListener pmOnDismissListener;
		boolean pmTouchable = true;
		boolean pmFocusable = true;
		boolean pmOutsideTouchable = true;
		public static final int DEFAULT_DISMISS_TIME = 1500;
		int animationStyle = 0;
	}
}
